<?php
class DataPoolTest extends PHPUnit_Framework_TestCase {

    public function testDataPool_testCreatePool () {
        $objContainer = new Container();
        $objDataPool = new DataPool($objContainer, 'TEST_POOL');

        $this->assertEquals(0, $objDataPool->getCurrentState());
        $this->assertEquals(32, strlen($objDataPool->getUid()));
        $this->assertEquals('Container', get_class($objDataPool->getContainer()));
        $this->assertEquals('TEST_POOL', $objDataPool->getName());
    }


    public function testDataPool_testCreatePoolRecovery () {
        $arSource = array (
            DataPool::KEY_DATA => array (
                0 => array (
                    'TEST_KEY' => 'TEST_VALUE'
                ),
                1 => array (
                    'TEST_KEY_NEXT' => 'TEST_VALUE_NEXT'
                )
            ),
            DataPool::KEY_UID => '56b6a9d2aeaea379a7e43808ffadf47a',
            DataPool::KEY_STATE => 2,
        );

        $objContainer = new Container();
        $objContainer->set('TEST_POOL', $arSource);

        $objDataPool = new DataPool($objContainer, 'TEST_POOL');

        $this->assertEquals(2, $objDataPool->getCurrentState());
        $this->assertEquals('56b6a9d2aeaea379a7e43808ffadf47a', $objDataPool->getUid());
        $this->assertEquals($arSource, $objDataPool->getContainer()->get('TEST_POOL'));
        $this->assertEquals('TEST_POOL', $objDataPool->getName());
    }


    public function testIsValidUid_Valid () {
        $objContainer = new Container();
        $objDataPool = new DataPool($objContainer, 'TEST_POOL');

        $this->assertTrue($objDataPool->isValidUid($objDataPool->getUid()));
    }


    public function testIsValidUid_NotValid () {
        $objContainer = new Container();
        $objDataPool = new DataPool($objContainer, 'TEST_POOL');

        $this->assertFalse($objDataPool->isValidUid('NOT_VALID_UID'));
    }


    public function testWritingInPool_Good () {
        $objContainer = new Container();
        $objDataPool = new DataPool($objContainer, 'TEST_POOL');

        $objDataPool->writing(array('TEST_KEY' => 'TEST_VALUE'));
        $objDataPool->writing(array('TEST_KEY_NEXT' => 'TEST_VALUE_NEXT'));

        $this->assertEquals(2, $objDataPool->getCurrentState());

        $arResult = $objContainer->get('TEST_POOL');

        $this->assertEquals('TEST_VALUE', $arResult[DataPool::KEY_DATA][0]['TEST_KEY']);
        $this->assertEquals('TEST_VALUE_NEXT', $arResult[DataPool::KEY_DATA][1]['TEST_KEY_NEXT']);
    }


    public function testWritingInPool_RecoveryGood () {
        $arSource = array (
            DataPool::KEY_DATA => array (
                '0' => array (
                    'TEST_KEY' => 'TEST_VALUE'
                ),
                '1' => array (
                    'TEST_KEY_NEXT' => 'TEST_VALUE_NEXT'
                )
            ),
            DataPool::KEY_UID => 'a88f3371ccbfdc97017edf7d844b72fd',
            DataPool::KEY_STATE => 2
        );

        $objContainer = new Container();
        $objContainer->set('TEST_POOL', $arSource);

        $objDataPool = new DataPool($objContainer, 'TEST_POOL');
        $objDataPool->writing(array('AFTER_RECOVERY' => 'NEW_DATA'));

        $arResult = $objDataPool->getContainer()->get('TEST_POOL');

        $this->assertEquals('TEST_VALUE', $arResult[DataPool::KEY_DATA][0]['TEST_KEY']);
        $this->assertEquals('TEST_VALUE_NEXT', $arResult[DataPool::KEY_DATA][1]['TEST_KEY_NEXT']);
        $this->assertEquals('NEW_DATA', $arResult[DataPool::KEY_DATA][2]['AFTER_RECOVERY']);
        $this->assertEquals(3, $objDataPool->getCurrentState());
    }


    public function testClean_Good () {
        $objContainer = new Container();
        $objDataPool = new DataPool($objContainer, 'TEST_POOL');

        $objDataPool->writing(array('TEST_KEY' => 'TEST_VALUE'));
        $objDataPool->writing(array('TEST_KEY_NEXT' => 'TEST_VALUE_NEXT'));
        $objDataPool->clean();

        $this->assertEquals(0, $objDataPool->getCurrentState());
        $this->assertEquals(32, strlen($objDataPool->getUid()));
        $this->assertEquals(new Container(), $objDataPool->getContainer());
        $this->assertEquals('TEST_POOL', $objDataPool->getName());
    }
}